#include "meet.hpp"
#include "db/sqlite_conn.hpp"

#include <mutex>

#include <math.h>

namespace bz
{

	REFLECTION(meet, id, name, bandwidth, 
		call_legprofile_id,meet_pwd, meet_num, auto_call, is_auto_recorder, is_auto_live, call_profile_id, default_layout, stream_url, show_mode, is_broadcast, is_fill, calls_id, start_time, is_auto_gen, poll_id, offline_check, sizes,fps)



	meet::meet():connected_num(0),
	 total_num(0),
	 online_num(0)
	{
		
	}

	meet::~meet()
	{
	}

	void meet::init()
	{
		ormpp_auto_key auto_key{
		"id"
		};
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		db->set_auto_key<meet>(auto_key);
	}

	bool meet::get()
	{
		

		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		
		auto res = db->query(FID(meet::id), "=", id);
		
		if (res.size() > 0) {
			*this = res[0];
			return true;
		}
		return false;
	}

	bool meet::is_allow_add_update(string& name, int id)
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);


		string sql = " where `name` = '" + name + "'";

		auto rows = db->query<meet>(sql);

		if (rows.size() < 1) {
			return true;
		}

		if (id > 0) {
			for (auto it : rows)
			{
				if (it.id != id) {
					return false;
				}
			}
			return true;
		}
		else {
			return false;
		}
	}

	

	vector<meet> meet::index()
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		vector<meet> res = db->query<meet>();
		return res;
	}

	int meet::add()
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		return db->insert(*this);
	}

	bool meet::remove()
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		return db->delete_records(FID(meet::id), "=", id);
	}

	int meet::update()
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		int res= db->update(*this);
	
		return res;
	}

	
}